package com.example.gaoyang.threadlocal;


import com.example.gaoyang.domain.SysUser;

public class UserThreadLocal {

    private static final ThreadLocal<SysUser> LOCAL = new ThreadLocal<>();

    /**
     * 将对象放入到ThreadLocal
     *
     * @param user
     */
    public static void set(SysUser user) {
        LOCAL.set(user);
    }

    /**
     * 返回当前线程中的SysUser对象
     *
     * @return
     */
    public static SysUser get() {
        return LOCAL.get();
    }
    /**
     * 返回当前线程中的SysUser对象
     *
     * @return
     */
    public static Long getUserId() {
        SysUser sysUser = get();
        if (sysUser == null) {
            return null;
        }
        return sysUser.getId();
    }

    /**
     * 删除当前线程中的SysUser对象
     */
    public static void remove() {
        LOCAL.remove();
    }

}
